package org.jeecgframework.codegenerate.generate.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jeecgframework.codegenerate.config.DbConfig;
import org.jeecgframework.codegenerate.database.DbReadTableUtil;
import org.jeecgframework.codegenerate.generate.IGenerate;
import org.jeecgframework.codegenerate.generate.TemplateConfig;
import org.jeecgframework.codegenerate.generate.pojo.ColumnVo;
import org.jeecgframework.codegenerate.generate.pojo.onetomany.MainTableVo;
import org.jeecgframework.codegenerate.generate.pojo.onetomany.SubTableVo;
import org.jeecgframework.codegenerate.util.NonceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.util.StrUtil;

public class CodeGenerateOneToMany extends CodeGenerate implements IGenerate {
 private static final Logger e = LoggerFactory.getLogger(CodeGenerateOneToMany.class);
 private static String f;
 public static String a = "A";
 public static String b = "B";
 private MainTableVo mainTableVo;
 private List<ColumnVo> mainColums;
 private List<ColumnVo> originalMainColumns;
 private List<SubTableVo> subTables;
 private static DbReadTableUtil k = new DbReadTableUtil();

 public CodeGenerateOneToMany(MainTableVo mainTableVo, List<SubTableVo> subTables) {
     this.subTables = subTables;
     this.mainTableVo = mainTableVo;
 }

 public CodeGenerateOneToMany(MainTableVo mainTableVo, List<ColumnVo> mainColums, List<ColumnVo> originalMainColumns, List<SubTableVo> subTables) {
     this.mainTableVo = mainTableVo;
     this.mainColums = mainColums;
     this.originalMainColumns = originalMainColumns;
     this.subTables = subTables;
 }

 public Map<String, Object> a() throws Exception {
     Map<String, Object> var1 = new HashMap();
     var1.put("bussiPackage", DbConfig.bussiPackage);
     var1.put("entityPackage", this.mainTableVo.getEntityPackage());
     var1.put("entityPackagePath", this.mainTableVo.getEntityPackage().replace(".", "/"));
     var1.put("entityName", this.mainTableVo.getEntityName());
     var1.put("tableName", this.mainTableVo.getTableName());
     var1.put("ftl_description", this.mainTableVo.getFtlDescription());
     var1.put("primaryKeyField", DbConfig.dbTableId);
     if (this.mainTableVo.getFieldRequiredNum() == null) {
         this.mainTableVo.setFieldRequiredNum(StringUtils.isNotEmpty(DbConfig.fieldRequiredNum) ? Integer.parseInt(DbConfig.fieldRequiredNum) : -1);
     }

     if (this.mainTableVo.getSearchFieldNum() == null) {
         this.mainTableVo.setSearchFieldNum(StringUtils.isNotEmpty(DbConfig.pageSearchFiledNum) ? Integer.parseInt(DbConfig.pageSearchFiledNum) : -1);
     }

     if (this.mainTableVo.getFieldRowNum() == null) {
         this.mainTableVo.setFieldRowNum(Integer.parseInt(DbConfig.fieldRowNum));
     }

     var1.put("tableVo", this.mainTableVo);

     try {
         if (this.mainColums == null || this.mainColums.size() == 0) {
             this.mainColums = DbReadTableUtil.a(this.mainTableVo.getTableName());
         }

         if (this.originalMainColumns == null || this.originalMainColumns.size() == 0) {
             this.originalMainColumns = DbReadTableUtil.readOriginalTableColumn(this.mainTableVo.getTableName());
         }

         var1.put("columns", this.mainColums);
         var1.put("originalColumns", this.originalMainColumns);
         Iterator var2 = this.originalMainColumns.iterator();

         while(var2.hasNext()) {
             ColumnVo var3 = (ColumnVo)var2.next();
             if (var3.getFieldName().toLowerCase().equals(DbConfig.dbTableId.toLowerCase())) {
                 var1.put("primaryKeyPolicy", var3.getFieldType());
             }
         }

         var2 = this.subTables.iterator();

         while(var2.hasNext()) {
             SubTableVo var12 = (SubTableVo)var2.next();
             List var4;
             if (var12.getColums() == null || var12.getColums().size() == 0) {
                 var4 = DbReadTableUtil.a(var12.getTableName());
                 var12.setColums(var4);
             }

             if (var12.getOriginalColumns() == null || var12.getOriginalColumns().size() == 0) {
                 var4 = DbReadTableUtil.readOriginalTableColumn(var12.getTableName());
                 var12.setOriginalColumns(var4);
             }

             String[] var13 = var12.getForeignKeys();
             ArrayList var5 = new ArrayList();
             String[] var6 = var13;
             int var7 = var13.length;

             for(int var8 = 0; var8 < var7; ++var8) {
                 String var9 = var6[var8];
                 var5.add(DbReadTableUtil.d(var9));
             }

             var12.setForeignKeys((String[])var5.toArray(new String[0]));
             var12.setOriginalForeignKeys(var13);
         }

         var1.put("subTables", this.subTables);
     } catch (Exception var10) {
         throw var10;
     }

     long var11 = NonceUtils.c() + NonceUtils.h();
     var1.put("serialVersionUID", String.valueOf(var11));
     e.info("code template data: " + var1.toString());
     return var1;
 }

 public List<String> generateCodeFile(String stylePath) throws Exception {
     Map var3 = this.a();
     String templatepath = DbConfig.templatepath;
     if (a(templatepath, "/").equals("jeecg/code-template")) {
    	 templatepath = "/" + a(templatepath, "/") + "/onetomany";
         DbConfig.setTemplatepath(templatepath);
     }

     TemplateConfig templateConfig = new TemplateConfig(templatepath);
     templateConfig.setStylePath(stylePath);
     if (this.mainTableVo != null && this.mainTableVo.getExtendParams() != null) {
    	 String vueStyle = (String)this.mainTableVo.getExtendParams().get("vueStyle");
    	 templateConfig.setVueStyle(StrUtil.isEmpty(vueStyle)? "vue" : vueStyle.trim());
     }

     this.loadTemplates(templateConfig, DbConfig.projectPath, var3);
     e.info("----- jeecg-boot ---- generate  code  success =======> 主表名：" + this.mainTableVo.getTableName());
     return this.msgs;
 }

 public List<String> generateCodeFile(String projectPath, String templatePath, String stylePath) throws Exception {
     if (projectPath != null && !"".equals(projectPath)) {
         DbConfig.setProjectPath(projectPath);
     }

     if (templatePath != null && !"".equals(templatePath)) {
         DbConfig.setTemplatepath(templatePath);
     }

     this.generateCodeFile(stylePath);
     return this.msgs;
 }
}
